<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="404" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hadd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形结构的部门数据 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="404" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hadd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children.length" @click.native="hDelete(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 实现部门的新增和编辑 -->
      <el-dialog
        :visible.sync="showDiaLog"
        :title="idEdit?'编辑部门':'新增子部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hCloseDialog"
      ><AddOrEdit
        ref="myson"
        :origin-list="originList"
        :cur-id="curId"
        :id-edit="idEdit"
        @success="hSuccess"
        @close="hClose"
      /></el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments } from '@/api/departments'
import { deDepartmentDetail } from '@/api/employees'
import { tranListTree } from '@/utils'
import AddOrEdit from './deptDialog'
export default {
  components: {
    AddOrEdit
  },
  data() {
    return {
      list: [],
      showDiaLog: false,
      curId: '',
      idEdit: false,
      originList: []
    }
  },

  created() {
    this.loadDepartments()
  },
  methods: {
    // 重置表单
    hCloseDialog() {
      // 重置表单 ---表单在子组件
      this.$refs.myson.resetForm()
    },
    // 添加部门
    hadd(id) {
      this.showDiaLog = true
      this.curId = id
      this.idEdit = false
    },
    // 编辑部门
    hEdit(id) {
      this.idEdit = true
      this.curId = id
      this.showDiaLog = true
      // 重新调用请求,刷新列表
      setTimeout(() => {
        this.$refs.myson.loadDepartmentDeatail()
      }, 0)
    },
    // 删除部门
    hDelete(id) {
      this.$confirm('此操作将永久删除此部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await deDepartmentDetail(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadDepartments()
        } catch (error) { console.log(error) }
      }).catch(() => {

      })
    },
    async loadDepartments() {
      try {
        const { data: res } = await getDepartments()
        console.log(res)
        // 删除第一个没有用的数据
        res.depts.shift()
        // tranListToTreeList(res.depts) 将数组中平铺的数据转换为树状数据
        this.list = tranListTree(res.depts)
        this.originList = res.depts.map(({ id, pid, code, name }) => { return { id, name, code, pid } })
        console.log(res)
        // 由于业务中传智教育的pid是:-1，它是最高级的节点
        // shift 从数组头部取出一个元素
      } catch (error) {
        console.log(error)
      }
    },
    hSuccess() {
      // 关闭弹层--->父组件
      this.showDiaLog = false
      // 重新渲染数据---> 父组件
      this.loadDepartments()
    },
    // 关闭弹层
    hClose() {
      this.showDiaLog = false
    }
  }
}
</script>
